package com.abhinav;

import java.util.Arrays;

/**
 * Java program to sort integer array using bubble sort sorting algorithm.
 * bubble sort is one of the simplest sorting algorithm but performance of
 * bubble sort is not good, its average and worst case performance ranges in
 * O(n2) and that's why it is not used to sort large set of unsorted data.
 * Bubble sort can be used for educational and testing purpose to sort small
 * number of data to avoid performance penalty. This program is also a good
 * example of how to print contents of Array in Java
 * 
 */

public class BubbleSort {
	public static void main(String args[]) {
		// testing our bubble sort method in Java
		int[] unsorted = { 32, 39, 21, 45, 23, 3 };
		bubbleSort(unsorted);

		// one more testing of our bubble sort code logic in Java
		int[] test = { 5, 3, 2, 1 };
		bubbleSort(test);

	}

	/*
	 * In bubble sort we need n-1 iteration to sort n elements at end of first
	 * iteration larget number is sorted and subsequently numbers smaller than
	 * that.
	 */
	public static void bubbleSort(int[] unsorted) {
		System.out.println("unsorted array before sorting : " + Arrays.toString(unsorted));

		// Outer loop - need n-1 iteration to sort n elements
		for (int i = 0; i < unsorted.length - 1; i++) {

			// Inner loop to perform comparison and swapping between adjacent numbers
			// After each iteration one index from last is sorted
			for (int j = 1; j < unsorted.length - i; j++) {

				// If current number is greater than swap those two
				if (unsorted[j - 1] > unsorted[j]) {
					int temp = unsorted[j];
					unsorted[j] = unsorted[j - 1];
					unsorted[j - 1] = temp;
				}
			}
			System.out.printf("unsorted array after %d pass %s: %n", i + 1, Arrays.toString(unsorted));
		}
	}

}
